/*******************************************************************************

  Paying Outsourced Labor: Direct Evidence from Linked Temp Agency-Worker-Client Data

  By Andres Drenik, Simon Jäger, Pascuel Plotkin and Benjamin Schoefer
	January 7th, 2021

	DESCRIPTION: Calculates the share of temp workers in 2005 at industry level

*******************************************************************************/




/********************************************************************************
***** Preliminaries
********************************************************************************/
set more off
cap log close
local curr_date = c(current_date)
log using "${logs}/14_Temp_Calculation_2005`curr_date'", replace


/****************************************************************************************
* Open the 2005 Dataset
****************************************************************************************/

	use "${Data_with_filter}/Argentina_Clean_2005.dta", clear

/****************************************************************************************
* Drop useless observations for our analysis
	* Drop duplicated observations for temp workers (modalidad = 102)
	* Only keep private sector workers
****************************************************************************************/

	drop if modalidad == 102
	drop if public_worker == 1

*************

	* Generate 2 digit industry code
	gen industry_code_2digit = int(ciiu_4/100)

	*Drop observations that don't register a wage
	drop if remuner_total == .

    *Flag Worker per date
	bys cuil_trab date (remuner_total) : gen worker = (_n == _N)
	gen total_obs = 1

	gcollapse (mean) year (sum) worker total_obs, by(date industry_code_2digit) fast

	*labels
	gen industry_code_letter     = 1 if inrange(industry_code_2digit, 1, 5)
	replace industry_code_letter = 2 if inrange(industry_code_2digit, 10, 14)
	replace industry_code_letter = 3 if inrange(industry_code_2digit, 40, 41)
	replace industry_code_letter = 4 if industry_code_2digit == 45
	replace industry_code_letter = 5 if inrange(industry_code_2digit, 15, 37)
	replace industry_code_letter = 6 if industry_code_2digit == 51
	replace industry_code_letter = 7 if industry_code_2digit == 50 | industry_code_2digit == 52
	replace industry_code_letter = 8 if inrange(industry_code_2digit, 60, 64)

	replace industry_code_letter = 10 if inrange(industry_code_2digit, 65, 67)
	replace industry_code_letter = 11 if inrange(industry_code_2digit, 70, 74)
	replace industry_code_letter = 12 if industry_code_2digit == 80 | industry_code_2digit == 85
	replace industry_code_letter = 13 if industry_code_2digit == 55
	replace industry_code_letter = 14 if inrange(industry_code_2digit, 90, 93) | industry_code_2digit==99 | industry_code_2digit==.

	replace industry_code_letter = 16 if industry_code_2digit == 75
	replace industry_code_letter = 17 if inrange(industry_code_2digit, 95, 97)
	replace industry_code_letter = 18 if industry_code_2digit == 0 | industry_code_2digit == .

	*RETAIL TRADE INCLUDES CAR TRADES
	label define industry 1 "Agriculture, Foresty, Fishing and Hunting" 2 "Mining" 3 "Utilities" 4 "Construction" 5 "Manufacturing" 6 "Wholsale Trade" 7 "Retail Trade" 8 "Transportation Warehousing and comunication" 9 "Information" 10 "Financial activities" 11 "Professional and Business Services" 12 "Education and Health Services" 13 "Leisure and Hospitality" 14 "Other Services (Excluding Public Administration)" 15 "Public Administration" 16 "Temporary work agents" 17 "Activities of private households as employers and undifferentiated production activities of private households" 18 "Other (public administration?)", modify
	label values industry_code_letter industry

	*Workers by industry
	egen workers_industry_letter = sum(worker), by(industry_code_letter)
	egen workers_industry_letter_year = sum(worker), by(industry_code_letter year)
	egen total_workers_year = sum(worker), by(year)
	egen avg_worker_year = total_workers_year/12
	gen perc_workers_industry_year = workers_industry_letter_year / total_workers_year

	sum perc_workers_industry_year if industry_code_letter == 16
	global temp_workers_2005 = r(mean)
	global perc_temp_2005 = ${temp_workers_2005}*100
	display("The percentage of temp workers in 2005 is: ${perc_temp_2005}")

log close
